package org.ihelpuoo.weboo.tag.ajax;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.ihelpuoo.I;
import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.parser.segment.Segment;

public class RootTag extends AjaxTag
{

	private static final long	serialVersionUID	= -6655007210698155785L;

	private List<String>		vars;
	private List<Segment>		views;

	private String				ready;

	public String getReady()
	{
		return ready;
	}

	public void setReady(String ready)
	{
		this.ready = ready;
	}

	public void add(ViewTag view)
	{
		vars.add(view.getName());
		views.add(view.render());
	}

	@Override
	protected void start() throws JspException
	{
		vars = new LinkedList<String>();
		views = new LinkedList<Segment>();
	}

	@Override
	protected void end() throws JspException
	{
		Segment seg = createSegment("ajax.root");
		seg.set("root",getContextPath()).set("extension",getWebooConfig().getActionSuffix());
		Iterator<String> it = vars.iterator();
		while (it.hasNext())
			seg.add("vars","var " + it.next() + ";" + I.NEW_LINE);

		Iterator<Segment> iv = views.iterator();
		while (iv.hasNext())
			seg.add("views",iv.next().toString());

		if (!StringUtils.isBlank(ready))
			seg.set("ready",ready);

		write(seg.toString());
	}

}
